<?php
namespace app\mxty\model;

use think\Model;
use think\model\concern\SoftDelete;
use app\common\enum\order\PayStatus as OrderPayStatusEnum;

// 预订表和场次表的中间表，记录一个预订订单中所有预订的场次
class BookingFragment extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $table = 'athlon_arena_booking_fragment';

    public function booking() 
    {
        return $this->belongsTo(Booking::class);
    }

    public function getPrice($date, $part_time_fragment_id)
    {
        return self::where([
            'part_time_fragment_id' => $part_time_fragment_id
        ])
            ->whereTime('start_date', '<=', $date)
            ->whereTime('end_date', '>=', $date)
            ->order('update_time desc')
            ->find();
    }

    public static function getBookingInfo($bookingFragmentId, $userId)
    {
        return self::with(['booking', 'booking.referees'])->where('id', $bookingFragmentId)->where('user_id', $userId)->where('pay_status', OrderPayStatusEnum::SUCCESS)->findOrEmpty();
    }
}
